    package Test;

    import java.util.Timer;
    import java.util.concurrent.PriorityBlockingQueue;

    class MyTask implements Comparable<MyTask>{
        //所欲需要定义一个类,这个类两个属性分别是时间和Runnable
       volatile  long time;
       volatile Runnable runnable;

        public MyTask(long time, Runnable runnable) {
            this.time = System.currentTimeMillis()+ time;//当前时间+等待的时间
            this.runnable = runnable;
        }

        @Override
        public int compareTo(MyTask o) {
           return (int) (this.time - o.time);
        }
        //还需要一个比较器

    }
    class MyTimer {

        //这里实现定时器的功能
        //构造方法里一直调用时间最近的线程
        private Object locker = new Object();//锁对象
        PriorityBlockingQueue<MyTask> blockingQueue = new PriorityBlockingQueue<>();//带阻塞效果的优先级队列

        public void schedule( Runnable runnable,long time) throws InterruptedException{
            MyTask myTask = new MyTask(time, runnable);
            blockingQueue.put(myTask);//此时就添加进堆了
            synchronized (locker) {
                locker.notify();
            }
        }

        public MyTimer() throws InterruptedException {
            Thread t = new Thread(() -> {
                synchronized (locker){
                       while (true) {
                           try {
                               long time = System.currentTimeMillis();
                           MyTask myTask = blockingQueue.take();
                           if (time >= myTask.time ) {
                                //此时可以直接运行此线程
                                myTask.runnable.run();
                            } else {
                               //如果没有到时间就
                               blockingQueue.put(myTask);//重新放回堆
                               locker.wait(myTask.time - System.currentTimeMillis());//最长时间阻塞
                           }
                           } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
            });
            t.start();
        }
    }
    public class Test6 {
        //实现定时器
        //定时器的原理就是可以控制他在什么时间执行
        //可以通过小根堆实现
        public static void main(String[] args) throws InterruptedException {
            MyTimer myTimer = new MyTimer();
            myTimer.schedule(new Runnable() {
                @Override
                public void run() {
                    System.out.println("hello4");
                }
            },4000);
            myTimer.schedule(new Runnable() {
                @Override
                public void run() {
                    System.out.println("hello3");
                }
            },3000);
            myTimer.schedule(new Runnable() {
                @Override
                public void run() {
                    System.out.println("hello2");
                }
            },2000);
            myTimer.schedule(new Runnable() {
                @Override
                public void run() {
                    System.out.println("hello1");
                }
            },1000);
            System.out.println("hello");
        }
    }

